package Test01_04;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.util.*;

/**
 * Created by 14229 on 2020/2/21.
 */
public class Test {
    public static void main(String[] args) throws DocumentException {
// 第一题
        List<Dept> depts = getDepts();
        for (Dept dept : depts) {
            System.out.println(dept);
        }

// 第二题
        List<Emp> topEmps = getTopEmps(depts);
        for (Dept dept : depts) {
            for (Emp topEmp : topEmps) {
                if (topEmp.getDeptid().equals(dept.getId())){
                    System.out.println("所有员工薪资最高的为："+dept.getName()+"   "+ topEmp.getName()+"   "+ topEmp.getSalary());
                }
            }
        }

//
// 第三题
        List<Map<String, String>> evg = getEvg(depts);
        for (Map<String, String> map : evg) {
            for (Map.Entry<String, String> entry : map.entrySet()) {
                System.out.println(entry.getKey()+"  "+entry.getValue());
            }
        }

// 第四题
        searchEmp("e09");

    }

// 第四题
    public static void searchEmp(String empId) throws DocumentException {
        List<Dept> depts = getDepts();
        for (Dept dept : depts) {
            List<Emp> list = dept.getList();
            for (Emp emp : list) {
                if (emp.getId().equals(empId)){
                    System.out.println(emp.getName()+"  "+emp.getId());
                    return;
                }
            }
        }
        System.out.println("没有找到");
    }

// 第三题
    public static List<Map<String,String>> getEvg(List<Dept> list){
        List<Map<String,String>> lim = new ArrayList<>();
        Map<String,String> map = new HashMap<>();
        for (Dept dept : list) {
            int sum = 0;
            int count = 0;
            List<Emp> list1 = dept.getList();
            for (Emp emp : list1) {
                count++;
                sum+=emp.getSalary();
            }
            map.put(dept.getName(), (sum/count)+" ");
        }
        lim.add(map);
        return lim;
    }

// 第二题
    public static List< Emp> getTopEmps(List<Dept> list){
        List<Emp> liste = new ArrayList<>();
        int max = 0;
        for (Dept dept : list) {
            List<Emp> em = dept.getList();
            for (Emp emp : em) {
                if (max<emp.getSalary()){
                    max=emp.getSalary();
                }
            }
        }

        for (Dept dept : list) {
            List<Emp> em = dept.getList();
            for (Emp emp : em) {
                if (max==emp.getSalary()){
                    liste.add(emp);
                }
            }
        }
        return liste;
    }

// 第一题
    public static List<Dept> getDepts() throws DocumentException {
        List<Dept> listd = new ArrayList<>();
        SAXReader sa = new SAXReader();
        Document read = sa.read(new File("dept.xml"));
        Element rootElement = read.getRootElement();
        List<Element> elements = rootElement.elements();
        for (Element element : elements) {
            String name = element.elementText("dept-name");
            String id = element.elementText("dept-id");
            listd.add(new Dept(name,id,new ArrayList<>()));
        }


        List<Emp> liste = new ArrayList<>();
        SAXReader sa1 = new SAXReader();
        Document read1 = sa1.read(new File("emp.xml"));
        Element rootElement1 = read1.getRootElement();
        List<Element> elements1 = rootElement1.elements();
        for (Element element : elements1) {
            String name = element.elementText("emp-name");
            String id = element.elementText("emp-id");
            String deptid = element.elementText("dept-id");
            String salary = element.elementText("emp-salary");
            liste.add(new Emp(name,id,deptid,Integer.parseInt(salary)));
        }
        Set<Emp> set = new HashSet<>(liste);
        set.addAll(liste);
        liste.clear();
        liste.addAll(set);


        for (Dept dept : listd) {
            for (Emp emp : liste) {
                if (dept.getId().equals(emp.getDeptid())){
                    dept.getList().add(emp);
                }
            }
        }

        return listd;
    }
}
